package com.wisemoresystem.seal.util.abchinaFinger.common;

/**
 * 说明：密码工具
 */

public class PasswordUtil {
	/**
	 * 输入：6位密码字符串 输出：转换后的4位密码 输入不标准时，返回""
	 * 
	 * @param prePassword
	 * @return
	 */
	public static String transform(String prePassword) {
		if (prePassword == null)
			return "";
		if (prePassword.length() != 6)
			return "";

		String PassWord = "";
		int i;
		int j;
		int k;
		int m;
		int n;
		int Num_Char;
		int Num_Int;
		String Pass = "0246897531abcdefghijklmzyxwvutsrqponMLKJIHGFEDCBANOPQRSTUVWXYZ";
		String Tmp_Char;
		int Counts;
		int Num_Passwd;
		int Surplus;
		int Quotient;
		int Quotient_Tmp;
		int Multiply_Base;

		int[] Seq = new int[4 + 1];
		String[] Base_Char = new String[62 + 1];
		int[] Base_Int = new int[10 + 1];
		int[] Base_Tmp = new int[6 + 1];

		Counts = 0;
		Num_Int = 10;
		Num_Char = 62;
		Num_Passwd = 6;
		Multiply_Base = 100000;

		for (i = 1; i <= 4; i = i + 1)
			Seq[i] = 0;

		for (j = 1; j <= Num_Int; j = j + 1)
			Base_Int[j] = j - 1;

		for (k = 1; k <= Num_Char; k = k + 1)
			Base_Char[k] = Pass.substring(k - 1, k);
		for (m = 1; m <= Num_Passwd; m = m + 1) {
			Tmp_Char = prePassword.substring(m - 1, m);
			for (n = 1; n <= Num_Char; n = n + 1) {
				if (Tmp_Char.equals(Base_Char[n]))
					break;
			}
			Base_Tmp[m] = n % Num_Int;
			if (Base_Tmp[m] == 0)
				Base_Tmp[m] = m;
		}
		Quotient = 0;
		for (i = 1; i <= Num_Passwd; i = i + 1) {
			Quotient = Quotient + Base_Tmp[i] * Multiply_Base;
			Multiply_Base = Multiply_Base / 10;
		}

		Quotient_Tmp = Quotient;

		while (Quotient_Tmp != 0) {
			Counts++;
			Surplus = Quotient_Tmp % Num_Char + 1;
			Quotient_Tmp = Quotient_Tmp / Num_Char;
			Seq[Counts] = Surplus;

		}
		if (Counts >= 3 && Counts < 4) {
			Counts = 4;
			Seq[Counts] = Quotient % Num_Char + 1;
		}

		PassWord = Base_Char[Seq[4]] + Base_Char[Seq[3]] + Base_Char[Seq[2]]
				+ Base_Char[Seq[1]];

		return PassWord;
	}
}
